package asia.yuanding.wechat.web.service.impl;

import asia.yuanding.wechat.web.entity.User;
import asia.yuanding.wechat.web.entity.UserContact;
import asia.yuanding.wechat.web.mapper.UserContactMapper;
import asia.yuanding.wechat.web.mapper.UserMapper;
import asia.yuanding.wechat.web.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import asia.yuanding.wechat.web.entity.SessionList;
import asia.yuanding.wechat.web.service.SessionListService;
import asia.yuanding.wechat.web.mapper.SessionListMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author YuanDing
* @description 针对表【session_list】的数据库操作Service实现
* @createDate 2025-08-03 23:01:18
*/
@Service
public class SessionListServiceImpl extends ServiceImpl<SessionListMapper, SessionList>
    implements SessionListService{


    @Resource
    private UserMapper userMapper;

    @Resource
    private UserContactMapper userContactMapper;



    /**
     * 创建会话
     * @param sessionList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createSession(SessionList sessionList) {
        String sessionId = sessionList.getSessionId();
        String contactId = sessionList.getContactId();
        String ownerId = sessionList.getCreateUser();
        String lastMessage = sessionList.getLastMessage();
        Date lastTime = sessionList.getLastTime();


        // 自己的信息
        User ownerUser = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUid, ownerId));

        // 好友的信息
        User contactUser = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUid, contactId));


        // 自己的信息
        UserContact ownerUserContact = userContactMapper.selectOne(new LambdaQueryWrapper<UserContact>()
                .eq(UserContact::getContactId, ownerId)
                .eq(UserContact::getUserId, contactId));

        // 好友的信息
        UserContact contactUserContact = userContactMapper.selectOne(new LambdaQueryWrapper<UserContact>()
                .eq(UserContact::getContactId, contactId)
                .eq(UserContact::getUserId, ownerId));

        SessionList ownerSessionList = new SessionList();

        ownerSessionList.setSessionId(sessionId);
        ownerSessionList.setContactName(contactUserContact.getContactRemark());
        ownerSessionList.setContactId(contactId);
        ownerSessionList.setIsShow(1);
        ownerSessionList.setLastMessage(lastMessage);
        ownerSessionList.setLastTime(lastTime);
        ownerSessionList.setLastName(contactUserContact.getContactRemark());
        ownerSessionList.setLastAvatar(contactUser.getAvatar());
        ownerSessionList.setContactId(contactId);
        ownerSessionList.setCreateTime(lastTime);
        ownerSessionList.setCreateUser(ownerId);

        // 保存下自己的
        this.save(ownerSessionList);


        ownerSessionList.setId(null);
        ownerSessionList.setContactName(ownerUserContact.getContactRemark());
        ownerSessionList.setContactId(ownerUserContact.getContactId());
        ownerSessionList.setLastName(ownerUserContact.getContactRemark());
        ownerSessionList.setLastAvatar(ownerUser.getAvatar());
        ownerSessionList.setCreateUser(contactId);

        // 保存下对方的
        this.save(ownerSessionList);

    }
}




